package Askgene::Database::Schema::Library;

use strict;
use warnings;

use base qw/DBIx::Class/;
use Reaction::Class;

use overload '""' => sub { shift->accession }, fallback => 1;

has accession   => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
has name        => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
has summary     => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
has description => ( isa => 'NonEmptyStr',       is => 'rw', required => 1 );
has created => ( isa => 'DateTime', is => 'ro' );
has entry_count => (
    isa     => 'PositiveInt',
    is      => 'rw',
    lazy    => '1',
    default => sub { shift->entries->count }
);

__PACKAGE__->load_components(qw/ UUIDColumns TimeStamp Core /);
__PACKAGE__->table('libraries');
__PACKAGE__->add_columns(
    accession => {
        'data_type' => 'varchar',
        'size'      => '36',
    },
    project_name => {
        'data_type' => 'varchar',
        'size'      => '50',
    },
    name => {
        'data_type' => 'varchar',
        'size'      => '50',
    },
    summary => {
        'data_type' => 'varchar',
        'size'      => '255',
    },
    description => {
        'data_type' => 'text',
        'size'      => undef,
    },
    created => {
        'data_type'     => 'timestamp',
        'set_on_create' => '1',
    },
);
__PACKAGE__->set_primary_key(qw/ project_name name /);
__PACKAGE__->add_unique_constraint( [qw/ accession /] );
__PACKAGE__->uuid_columns('accession');

__PACKAGE__->belongs_to(
    'project' => 'Askgene::Database::Schema::Project',
    { 'foreign.name' => 'self.project_name' }
);
__PACKAGE__->has_many(
    'entries' => 'Askgene::Database::Schema::Entry',
    {   'foreign.project_name' => 'self.project_name',
        'foreign.lib_name'     => 'self.name'
    }
);

1;
